package com.bawei.yuekao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.yuekao.R;
import com.bawei.yuekao.entity.NetSong;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * @Author : yaotianxue
 * @Time : On 2023/7/8 08:44
 * @Description : NetSongAdapter
 */
public class NetSongAdapter extends BaseAdapter {
    private Context mContext;

    public NetSongAdapter(Context context, List<NetSong.DataBean> list) {
        mContext = context;
        mList = list;
    }

    private List<NetSong.DataBean> mList;

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    //为什么复用？？有1000个条目 不复用的话会创建1000个item布局，若复用一个页面展示10个item，只会创建10个item布局
    //现实案例：餐馆吃饭一共10张桌子，100个客户分批来吃饭，不需要准备100张桌子，复用10张桌子
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null){
            convertView  = LayoutInflater.from(mContext).inflate(R.layout.item,null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //查找控件
        viewHolder.iv = convertView.findViewById(R.id.iv);
        viewHolder.tv_name = convertView.findViewById(R.id.tv_name);
        viewHolder.tv_singer = convertView.findViewById(R.id.tv_singer);


        //数据绑定
        Glide.with(mContext).load(mList.get(position).getAlbum()).into(viewHolder.iv);
        viewHolder.tv_name.setText(mList.get(position).getTitle()+"");
        viewHolder.tv_singer.setText(mList.get(position).getArtist()+"");

        return convertView;
    }
    //桌子
    class ViewHolder{
        ImageView iv;
        TextView tv_name,tv_singer;
    }




}
